<template>
  <div class="course-user-page">
    <div class="head">帖子评论</div>
    <div class="select-button">
      <el-row>
        <el-col :span="12">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-input v-model="inputContent" placeholder="请输入评论内容关键词" @change="pageComments" ></el-input>
            </el-col>
            <el-col :span="12">
              <el-input v-model="inputPostId" placeholder="请输入帖子id"  @change="pageComments"></el-input>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="3" :offset="1">
          <el-button  type="primary" icon="el-icon-search" @click="pageComments()">查询</el-button>
        </el-col>
        <el-col :span="3">
          <el-button  type="primary" icon="el-icon-search" @click="clear()">清空</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="student-list" style="display: flex;flex-direction: column;align-items: center">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="selection" width="55"  align="center">
        </el-table-column>
        <el-table-column fixed prop="id" label="评论ID" min-width="50%" align="center">
        </el-table-column>
        <el-table-column prop="nickname" label="用户昵称" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="title" label="帖子标题" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="content" label="评论内容" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="reply" label="作者回复" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="visibility" label="可见性" min-width="40%" align="center" >
          <template slot-scope="scope">
            <div>{{visibilityTypes[scope.row.visibility]}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="gmtCreate" label="发布时间" min-width="50%" align="center" >
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="250">
          <template slot-scope="scope">
            <el-button type="text" size="medium" @click="showVisibilityModel(scope.row.id,scope.$index)">可视权限</el-button>
            <el-button type="text" size="medium" @click="showReplyModel(scope.row.id,scope.$index)">回复评论</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block" style="margin-top: 20px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[8, 10, 20, 40]"
          :page-size="8"
          layout="total, sizes, prev, pager, next, jumper"
          :total="parseInt(total)">
        </el-pagination>
      </div>
    </div>


    <el-dialog title="操作" show-close :visible.sync="visibilityVisible">
      <el-form label-width="120px">
        <el-form-item label="可视权限">
          <el-select v-model="visibilityType" filterable style="min-width: 450px">
            <el-option
                v-for="(item,index) in visibilityTypes"
                :key="item"
                :label="item"
                :value="index">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="visibilityVisible = false">取 消</el-button>
        <el-button type="primary" @click="upgrade">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog title="操作" show-close :visible.sync="replyVisible">
      <el-form label-width="120px">
        <el-form-item label="评论回复">
          <el-input v-model="inputReply"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="replyVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "IslandManageView",
  data() {
    return {
      total: 0,
      currentSize: 8,
      currentPage: 1,
      visibilityType:0,
      visibilityTypes:["仅自己可见","仅关注可见","全部可见"],

      visibilityVisible: false,
      replyVisible: false,

      input: "",
      inputReply: "",
      inputUpdateVisibility: '',
      inputPostId: "",
      inputContent: "",
      inputUserId: "",
      inputVisibility: "",
      debounceClock: null,
      islandId:  "",

      commentId : "",
      auditId : "",

      tableData: [],
    };
  },
  mounted() {
    this.islandId = this.$route.params.id
    this.commentId = this.$route.query.linkId
    this.auditId = this.$route.query.auditId
    this.pageComments();
  },
  methods: {
    pageComments() {
      this.$api.pageIslandComments({
        pageSize: this.currentSize,
        pageNow: this.currentPage,
        islandId : this.islandId,
        content: this.inputContent || '',
        postId: this.inputPostId || '',
        id : this.commentId || ''
      })
        .then((res) => {
          let list = res.data.rows;
          this.total = res.data.totalPage
          console.log(this.total)
          this.tableData = list
          for (let i = 0; i < list.length; i++) {
            list[i].gmtCreate = this.gmtToDate(list[i].gmtCreate)
          }
        });
    },
    handleSizeChange(val) {
      this.currentSize = parseInt(val);
      this.pageComments();
    },
    handleCurrentChange(val) {
      this.currentPage = parseInt(val);
      this.pageComments();
    },
    clear(){
      this.inputContent = ''
      this.input = ''
      this.inputPostId = ''
      this.pageComments();
    },
    showVisibilityModel(commentId,index) {
      this.currentBindCommentId = commentId
      this.visibilityVisible = true
      this.visibilityType = this.tableData[index].visibility
      this.inputReply = this.tableData[index].replay
    },
    showReplyModel(commentId,index){
      this.currentBindCommentId = commentId
      this.replyVisible = true
      this.inputReply = this.tableData[index].reply
    },


    upgrade() {
      this.$api.upgrade({
        auditId: this.auditId,
        linkId : this.currentBindCommentId,
        visibility: this.visibilityType,
        type : 5,// 对于后端AuditContentTypeEnum枚举值
      }).then(res => {
        this.visibilityVisible = false
        this.pageComments()
      })
    },

    update() {
      this.$api.updateComment({
        id: this.currentBindCommentId,
        reply:this.inputReply
      }).then(res => {
        console.log(res)
        this.replyVisible = false
        this.pageComments()
      })
    },


    gmtToDate(gmt) {
      let fmt = "YYYY-mm-dd HH:MM";
      let date = new Date(parseInt(gmt));
      let ret;
      const opt = {
        "Y+": date.getFullYear().toString(), // 年
        "m+": (date.getMonth() + 1).toString(), // 月
        "d+": date.getDate().toString(), // 日
        "H+": date.getHours().toString(), // 时
        "M+": date.getMinutes().toString(), // 分
        "S+": date.getSeconds().toString(), // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
      };
      for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
          fmt = fmt.replace(
            ret[1],
            ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
          );
        }
      }
      return fmt;
    }
  },
};
</script>

<style scoped>
.course-user-page{
  margin-top: 20px;
  width: 100%;
}


.head {
  padding-bottom: 10px;
}

.select-button {
  padding: 10px 0;
}

.student-list {
  width: 100%;
  padding: 10px 0;
  display: flex;
}
</style>
